"""
Created on 21 mrt. 2012

@author: Daan
"""
import unittest
import os
import shutil
import xmlrpclib

from unittests.stubs import builtin_open, stub_bases

from pCentralServer import CentralServer
from pCentralServer.plugins import FileStorage

TEST_DIR = os.path.join(os.path.dirname(__file__), "dat")
UNITTEST_OUTPUT_DIR = os.path.join(TEST_DIR, "output")

#noinspection PyCallByClass,PyTypeChecker
class FileStorageTester(unittest.TestCase):
    verbose = True

    def setUp(self):
        """
        DOCSTRING
        """

        if not os.path.isdir(UNITTEST_OUTPUT_DIR):
            os.makedirs(UNITTEST_OUTPUT_DIR)

    def tearDown(self):
        """
        DOCSTRING
        """
        shutil.rmtree(UNITTEST_OUTPUT_DIR, True)

    def test_01_instantiation(self):
        """
        DOCSTRING
        """

        with stub_bases.stub_bases(CentralServer.CentralServer, FileStorage.FileStorage):
            testObject = CentralServer.CentralServer(UNITTEST_OUTPUT_DIR)

            self.assertTrue(isinstance(testObject, CentralServer.CentralServer))
            self.assertTrue(isinstance(testObject, FileStorage.FileStorage))

    def test_02a_store_files(self):
        """
        DOCSTRING
        """

        with stub_bases.stub_bases(CentralServer.CentralServer, FileStorage.FileStorage):
            testObject = CentralServer.CentralServer(UNITTEST_OUTPUT_DIR)

            binaryHandle = xmlrpclib.Binary(open(os.path.join(TEST_DIR, "file.bin")).read())

            self.assertTrue(FileStorage.FileStorage.storeFile(testObject, "V0900", {"car": 1}, binaryHandle))

            self.assertTrue(os.path.exists(os.path.join(UNITTEST_OUTPUT_DIR, "V0900.info")))
            self.assertTrue(os.path.exists(os.path.join(UNITTEST_OUTPUT_DIR, "V0900.cont")))

    def test_02b_store_files_fail(self):
        """
        DOCSTRING
        """

        with stub_bases.stub_bases(CentralServer.CentralServer, FileStorage.FileStorage):
            testObject = CentralServer.CentralServer(UNITTEST_OUTPUT_DIR)

            binaryHandle = xmlrpclib.Binary(open(os.path.join(TEST_DIR, "file.bin")).read())

            with builtin_open.builtin_open(verbose=self.verbose).open_fixed_ioerror(
                [os.path.join(UNITTEST_OUTPUT_DIR, "V0900.info")]):
                self.assertFalse(FileStorage.FileStorage.storeFile(testObject, "V0900", {"car": 1}, binaryHandle))

                self.assertFalse(os.path.exists(os.path.join(UNITTEST_OUTPUT_DIR, "V0900.info")))
                self.assertFalse(os.path.exists(os.path.join(UNITTEST_OUTPUT_DIR, "V0900.cont")))

            with builtin_open.builtin_open(verbose=self.verbose).open_fixed_ioerror(
                [os.path.join(UNITTEST_OUTPUT_DIR, "V0900.cont")]):
                self.assertFalse(FileStorage.FileStorage.storeFile(testObject, "V0900", {"car": 1}, binaryHandle))

                self.assertFalse(os.path.exists(os.path.join(UNITTEST_OUTPUT_DIR, "V0900.info")))
                self.assertFalse(os.path.exists(os.path.join(UNITTEST_OUTPUT_DIR, "V0900.cont")))

    def test_03a_retrieve_files(self):
        """
        DOCSTRING
        """

        with stub_bases.stub_bases(CentralServer.CentralServer, FileStorage.FileStorage):
            testObject = CentralServer.CentralServer(TEST_DIR)

            info, xmlrpcBinary = FileStorage.FileStorage.retrieveFile(testObject, "V0900")

            self.assertEqual({"car": 1}, info)
            self.assertTrue(len(xmlrpcBinary.data) > 0)
            self.assertEqual(xmlrpcBinary.data, open(os.path.join(TEST_DIR, "file.bin")).read())

    def test_03b_retrieve_files_fail(self):
        """
        DOCSTRING
        """

        with stub_bases.stub_bases(CentralServer.CentralServer, FileStorage.FileStorage):
            testObject = CentralServer.CentralServer(UNITTEST_OUTPUT_DIR)

            with builtin_open.builtin_open(verbose=self.verbose).open_fixed_ioerror(
                [os.path.join(TEST_DIR, "V0900.info")]):
                info, content = FileStorage.FileStorage.retrieveFile(testObject, "V0900")

                self.assertEqual(info, None)
                self.assertEqual(content, None)

            with builtin_open.builtin_open(verbose=self.verbose).open_fixed_ioerror(
                [os.path.join(TEST_DIR, "V0900.cont")]):
                info, content = FileStorage.FileStorage.retrieveFile(testObject, "V0900")

                self.assertEqual(info, None)
                self.assertEqual(content, None)

if __name__ == "__main__":
    unittest.main()